package xps.and.uudaijia.userclient.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.jude.utils.JUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.data.baen.SmsResponce;
import xps.and.uudaijia.userclient.data.net.UserNetWorks;
import xps.and.uudaijia.userclient.view.MainActivity;
import xps.and.uudaijia.userclient.view.base.BaseActivity;

/**
 * Created by Administrator on 2017/6/29.
 */

public class ModifyActivity extends BaseActivity {
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.Oldpassword_Edit)
    EditText OldpasswordEdit;
    @BindView(R.id.Newpassword_Edit)
    EditText NewpasswordEdit;
    @BindView(R.id.OKpassword_Edit)
    EditText OKpasswordEdit;
    @BindView(R.id.OK_Button)
    Button OKButton;

    @Override
    protected int getLayout() {
        return R.layout.activity_modify;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
    }

    @OnClick({R.id.actionbar_iv_back, R.id.OK_Button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.OK_Button:
                if (NewpasswordEdit.getText().toString().equals(OKpasswordEdit.getText().toString())) {
                    UpdatePassword();
                } else {
                    JUtils.Toast("两次密码不同");
                }
                break;

        }
    }

    void UpdatePassword() {
        UserNetWorks.UpdatePassword(OldpasswordEdit.getText().toString(), OKpasswordEdit.getText().toString(), new Subscriber<SmsResponce>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(SmsResponce smsResponce) {
                if (smsResponce.getReturn_code().equals("SUCCESS")) {
                    Intent intent = new Intent(ModifyActivity.this, MainActivity.class);
                    startActivity(intent);
                    JUtils.Toast(smsResponce.getReturn_msg());
                    SharedPreferences sp = JUtils.getSharedPreference();
                    // 写入配置文件
                    SharedPreferences.Editor spEd = sp.edit();
                    spEd.putString("password", OKpasswordEdit.getText().toString());
                    spEd.commit();
                    MainActivity homeActivity = new MainActivity();
                    homeActivity.finish();
                    finish();
                } else if (smsResponce.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(smsResponce.getReturn_msg());
                }
            }
        });

    }
}
